*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% PROGRAMS
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


	****************************************
	* TABLE STATS
	****************************************

	
	capt prog drop stats_table_a
	program stats_table_a, eclass
	
		syntax varlist

		qui {
		
			foreach y of local varlist {
			
				estadd scalar Nc = e(N_clust)

				sum `y' if D == 0 & e(sample) == 1
				local stat = r(mean)
				estadd scalar Cmean = r(mean)
				
			}
		}

	end
	
	
	*T-test pvalues
	capt prog drop stats_table_pvalue
	program stats_table_pvalue, eclass
	
		syntax varlist

		qui {
		
			foreach y of local varlist {
				test F2event  L1event 
				local stat = r(p)
				estadd scalar test_diff1 = r(p)
				
				test F2event = L2event 
				local stat = r(p)
				estadd scalar test_diff2 = r(p)
				
				test F2event = L3event 
				local stat = r(p)
				estadd scalar test_diff3 = r(p)
				
				test L1event = L2event
				local stat = r(p)
				estadd scalar test_diff4 = r(p)

				test L2event = L3event 
				local stat = r(p)
				estadd scalar test_diff5 = r(p)
				
				

		}
		}

	end
	
	
	****************************************
	* BALANCE TABLE
	****************************************

		capt prog drop rnd_check
		program rnd_check, eclass

		syntax varlist [if] [in], [ * ]
		tempname obs m_mu m_mu_1  m_mu_2 m_sd m_sd_1 m_sd_2 m_coef_0 m_d_se_0 m_d_p_0                    
							   
		foreach var of local varlist {
			
			qui ttest `var', by($treat) `options'
			
				mat `m_mu' = nullmat(`m_mu'), (r(mu_1)*r(N_1)+r(mu_2)*r(N_2))/(r(N_1)+r(N_2))
				mat `m_mu_1' = nullmat(`m_mu_1'), r(mu_1)
				mat `m_mu_2' = nullmat(`m_mu_2'), r(mu_2)
				mat `m_sd' = nullmat(`m_sd'), r(sd)
				mat `m_sd_1' = nullmat(`m_sd_1'), r(sd_1)
				mat `m_sd_2' = nullmat(`m_sd_2'), r(sd_2)
				mat `obs' = nullmat(`obs'), r(N_1)+r(N_2)
				mat `obs' = nullmat(`obs'), r(N_1)
				mat `obs' = nullmat(`obs'), r(N_2)
			
			qui xi: reg `var' $treat , robust
			
				matrix b = e(b)
				matrix v = e(V)
				mat `m_coef_0' = nullmat(`m_coef_0'), b[1,1]
				mat `m_d_se_0' = nullmat(`m_d_se_0'), sqrt(v[1,1])
				mat `m_d_p_0'  = nullmat(`m_d_p_0'), 2*ttail(e(df_r),abs(b[1,1]/sqrt(v[1,1])))
							
		}
			 
		foreach mat in obs m_mu m_mu_1  m_mu_2 m_sd m_sd_1 m_sd_2 m_coef_0 m_d_se_0 m_d_p_0       {
			mat coln ``mat'' = `varlist'
		}

		tempname b V
		mat `b' = `m_mu'*0
		mat `V' = `b''*`b'
		eret post `b' `V'
		eret local cmd "rnd_check"
		
		mat temp = `obs'
		eret scalar N1 = temp[1,1] 
		eret scalar N2 = temp[1,2] 
		eret scalar N3 = temp[1,3] 
		eret scalar N4 = temp[1,4] 
			
		foreach mat in obs m_mu m_mu_1  m_mu_2 m_sd m_sd_1 m_sd_2 m_coef_0 m_d_se_0 m_d_p_0      {
			eret mat `mat' = ``mat''
		}
		
		end

		
		
